#!/bin/bash
# Copyright (C) 2017 Ingrasys, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

VERSION="1.0.0"
TRUE=200
FALSE=404

TYPE=${1}
PORT=${2}

#FIXME update SI values
#QSFP Optical
function _qsfp_optical_si_set {
    local idx=0
    #FIXME
    local si=(11 68 13    15 68 15    11 68  9    11 68 13
              11 68 17    11 68 13    13 68 11    11 68 15
              11 68 13    11 68 17    11 68 17    13 68 13
              11 68 15    11 68  9    11 68  9    11 68  7
               9 68 13    13 68  7    11 68  7    11 68 11
              11 68 13    11 68 13    9  68  7    11 68 11
              11 68  9    11 68 15    11 68  9    11 68  9
              11 68  9    11 68  7    11 68  9    11 68  9 
              13 68  9    13 68 13    13 68  7    13 68 11 
              13 68  5    13 68 11    13 68  5    13 68 11
              
              11 68  3    11 68  9    11 68  5    11 68 13
              11 68  9    11 68  9    11 68 11    13 68  5              
              13 68  3    11 68  7    11 68  5    11 68  9              
              11 68  9    13 68  9    13 68  1    11 68 11
               5 68  1    11 68  7    11 68  1    13 68  3
              11 68  5    11 68  3    11 68  7    11 68  3              
              11 68  3    13 68 11    13 68  1    13 68  3
              11 68  1    13 68  1    13 68  1    11 68  7              
              11 68  1    11 68  5    11 68  1     9 68  5
               5 68  1    13 68  1    11 68  5    11 68  3
               
              11 68  1    11 68 11    11 68  1    13 68  7
              13 68  1    15 68  1    11 68  1    13 68  7
              13 68  1    11 68  5    11 68  1    11 68  1
              11 68  5    13 68  3    11 68  5     9 68  1
              11 68  5    13 68  5    13 68  1    11 68  9
              13 68  1    11 68 13    13 68  1    11 68  7
              11 68  1    11 68  1     9 68  1    11 68  1
              11 68  1    11 68  1    11 68  5    11 68  1
              13 68  1    11 68 11    11 68  1    11 68  1
              11 68  1    11 68 15    11 68  1    13 68  5

              11 68  1    11 68  1     9 68  1    13 68  1 
              11 68  1    11 68  1     9 68  1    11 68  1 
              11 68  1    13 68  3    11 68  1    13 68  1
              11 68  1    13 68  1    11 68  1    11 68 13 
              11 68  1    11 68  1    11 68  1    11 68  1 
              11 68 11    11 68  1    13 68  1    11 68  1
              11 68  1    11 68  9    11 68  1    11 68  7 
              11 68  1    13 68  1    13 68  1    11 68  1 
              11 68  1    11 68  5    11 68  1    13 68  3
              11 68  3     9 68  1    11 68  3    11 68  1

              11 68  7    11 68  9    11 68  1   11 68  9
              11 68  3    11 68 15    11 68  3    9 68  9
               9 68  1    11 68  3    11 68  1   11 68  5
              11 68  5     5 68  5    11 68  5   11 68  1
              13 68  5    13 68  5    11 68  1   11 68  9
              11 68  1    11 68  1    11 68  1   13 68  7
              11 68  1    11 68  5    11 68  1   11 68  3
              11 68  5    11 68  1    11 68  5   11 68  1
              11 68  3    11 68  7    11 68  3   11 68  9
              11 68  1    13 68  7    11 68  1    9 68  9
                                                 
              13 68  1    11 68  5    11 68  3   13 68  5
              11 68  7    11 68 11    13 68  5    9 68  5
              11 68  5    11 68 13    13 68  7   11 68 13
              11 68  7    11 68 11    11 68  5   11 68 11
              11 68 11    11 68 11    11 68  7   11 68 11
              11 68 13    11 68 11    11 68  9   15 68  9
              11 68 11    11 68  5    11 68  7   13 68 11
              11 68  9    13 68 17    11 68  7   11 68 13
              11 68 11    11 68 17    11 68 13   11 68 15
              13 68  9    13 68 11    11 68 11   13 68  7
              
              11 68 13    13 68  9    11 68  9   11 68  9
              11 68 19    11 68 15    11 68  9   11 68 11
              13 68 13    13 68 17    13 68 11   11 68 11
              13 68  9    13 68 13    11 68 11   11 68  9)
    
    idx=$(( PORT * 12 ))     
    #FIXEME
    #for j in {0..3}
    #do
    #    bcmcmd "phy ce${PORT} CL93N72_UT_CTL2r.${j} CL93N72_TXFIR_POST=${si[idx++]}; phy ce${PORT} CL93N72_UT_CTL3r.${j} CL93N72_TXFIR_MAIN=${si[idx++]}; phy ce${PORT} CL93N72_UT_CTL2r.${j} CL93N72_TXFIR_PRE=${si[idx++]};"                           
    #done
}

function _qsfp_dac_si_set {
    local idx=0           
    # each port has 4 lanes, each lane has 3 SI values (post,main,pre), total 4*3=12
    local port_si=12
    
    #si[0]: txfir_post for port0 lane0
    #si[1]: txfir_main for port0 lane0 
    #si[2]: txfir_pre  for port0 lane0    
    
    #si[3]: txfir_post for port0 lane1
    #si[4]: txfir_main for port0 lane1 
    #si[5]: txfir_pre  for port0 lane1    
    
    local si=(11 68 13    15 68 15    11 68  9    11 68 13
              11 68 17    11 68 13    13 68 11    11 68 15
              11 68 13    11 68 17    11 68 17    13 68 13
              11 68 15    11 68  9    11 68  9    11 68  7
               9 68 13    13 68  7    11 68  7    11 68 11
              11 68 13    11 68 13    9  68  7    11 68 11
              11 68  9    11 68 15    11 68  9    11 68  9
              11 68  9    11 68  7    11 68  9    11 68  9 
              13 68  9    13 68 13    13 68  7    13 68 11 
              13 68  5    13 68 11    13 68  5    13 68 11
              
              11 68  3    11 68  9    11 68  5    11 68 13
              11 68  9    11 68  9    11 68 11    13 68  5              
              13 68  3    11 68  7    11 68  5    11 68  9              
              11 68  9    13 68  9    13 68  1    11 68 11
               5 68  1    11 68  7    11 68  1    13 68  3
              11 68  5    11 68  3    11 68  7    11 68  3              
              11 68  3    13 68 11    13 68  1    13 68  3
              11 68  1    13 68  1    13 68  1    11 68  7              
              11 68  1    11 68  5    11 68  1     9 68  5
               5 68  1    13 68  1    11 68  5    11 68  3
               
              11 68  1    11 68 11    11 68  1    13 68  7
              13 68  1    15 68  1    11 68  1    13 68  7
              13 68  1    11 68  5    11 68  1    11 68  1
              11 68  5    13 68  3    11 68  5     9 68  1
              11 68  5    13 68  5    13 68  1    11 68  9
              13 68  1    11 68 13    13 68  1    11 68  7
              11 68  1    11 68  1     9 68  1    11 68  1
              11 68  1    11 68  1    11 68  5    11 68  1
              13 68  1    11 68 11    11 68  1    11 68  1
              11 68  1    11 68 15    11 68  1    13 68  5

              11 68  1    11 68  1     9 68  1    13 68  1 
              11 68  1    11 68  1     9 68  1    11 68  1 
              11 68  1    13 68  3    11 68  1    13 68  1
              11 68  1    13 68  1    11 68  1    11 68 13 
              11 68  1    11 68  1    11 68  1    11 68  1 
              11 68 11    11 68  1    13 68  1    11 68  1
              11 68  1    11 68  9    11 68  1    11 68  7 
              11 68  1    13 68  1    13 68  1    11 68  1 
              11 68  1    11 68  5    11 68  1    13 68  3
              11 68  3     9 68  1    11 68  3    11 68  1

              11 68  7    11 68  9    11 68  1    11 68  9
              11 68  3    11 68 15    11 68  3     9 68  9
               9 68  1    11 68  3    11 68  1    11 68  5
              11 68  5     5 68  5    11 68  5    11 68  1
              13 68  5    13 68  5    11 68  1    11 68  9
              11 68  1    11 68  1    11 68  1    13 68  7
              11 68  1    11 68  5    11 68  1    11 68  3
              11 68  5    11 68  1    11 68  5    11 68  1
              11 68  3    11 68  7    11 68  3    11 68  9
              11 68  1    13 68  7    11 68  1     9 68  9
                                                  
              13 68  1    11 68  5    11 68  3    13 68  5
              11 68  7    11 68 11    13 68  5     9 68  5
              11 68  5    11 68 13    13 68  7    11 68 13
              11 68  7    11 68 11    11 68  5    11 68 11
              11 68 11    11 68 11    11 68  7    11 68 11
              11 68 13    11 68 11    11 68  9    15 68  9
              11 68 11    11 68  5    11 68  7    13 68 11
              11 68  9    13 68 17    11 68  7    11 68 13
              11 68 11    11 68 17    11 68 13    11 68 15
              13 68  9    13 68 11    11 68 11    13 68  7
                                                  
              11 68 13    13 68  9    11 68  9    11 68  9
              11 68 19    11 68 15    11 68  9    11 68 11
              13 68 13    13 68 17    13 68 11    11 68 11
              13 68  9    13 68 13    11 68 11    11 68  9)
    
    # get first si index for this port
    idx=$(( PORT * port_si ))     
    
    # loop for 4 lanes
    for j in {0..3}
    do
        bcmcmd "phy ce${PORT} CL93N72_UT_CTL2r.${j} CL93N72_TXFIR_POST=${si[idx++]}; phy ce${PORT} CL93N72_UT_CTL3r.${j} CL93N72_TXFIR_MAIN=${si[idx++]}; phy ce${PORT} CL93N72_UT_CTL2r.${j} CL93N72_TXFIR_PRE=${si[idx++]};"
    done
}

# Help usage function
function _help {
    echo "========================================================="
    echo "# Description: Help Function"
    echo "========================================================="
    echo "----------------------------------------------------"
    echo "EX       : ${0} help"
    echo "         : ${0} optical [0-63]"
    echo "         : ${0} dac [0-63]"
    echo "----------------------------------------------------"
}

#Main Function
function _main {

    if [ "${TYPE}" == "help" ]; then
        _help
    elif [ "${TYPE}" == "optical" ]; then
        _qsfp_optical_si_set
    elif [ "${TYPE}" == "dac" ]; then
        _qsfp_dac_si_set
    else
        echo "Invalid Parameters, Exit!!!"
        _help
        exit ${FALSE}
    fi
}

_main
